package Mike.FileOS;
import java.awt.*;
import java.net.*;
import java.awt.image.*;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import java.io.*;

public class WriteImage {
	private static Image image;
	private static int DEFAULT_WIDTH=100;
	private static int DEFAULT_HEIGHT=100;
	private static String debugurl="";
	//private static int DEFAULT_NOPIC_PATH="";
	public static String Write_Image(String source,String destination){
		//load image		
		String source2="";
		Toolkit toolkit = Toolkit.getDefaultToolkit();
		debugurl=source;
		try{
			image = toolkit.getImage(new URL(source));
		}catch(MalformedURLException e){
			System.out.println("URL Exception"+e);
		}
		//bufferimage
		BufferedImage outImage =toBufferedImage(image);
		if(outImage==null){
			System.out.println("Error 404 : Image Not Found : "+debugurl);
//			String[] temp=destination.split("\\\\");
//			String source2="";
//			for(int i=0;i<temp.length-2;i++){
//				source2+=temp[i]+"\\";
//			}
			source2=shop.alldefault.AllDefault.getQUERY_URL()+"nopicture.jpg";
			try {
				image = toolkit.getImage(new URL(source2));
			} catch (MalformedURLException e) {
				e.printStackTrace();
			}
			outImage =toBufferedImage(image);
		}else{}
		//save image
		Mike.FileOS.WriteFile.buildpath(destination);
		File tempFile=new File(destination);
		try {
			ImageIO.write(outImage,"jpg",tempFile);
			return source2;
		} catch (IOException e1) {
			e1.printStackTrace();
		}

		return "";
	}
	public static BufferedImage toBufferedImage(Image image) {
		if (image instanceof BufferedImage) {
			return (BufferedImage)image;
		}else if(image==null){
			return null;
		}
		// Get height width & check before
		int counter=0;
		while(image.getWidth(null)==-1){if(counter>10000000)break;else counter++;}
			if(counter<10000000){
				while(image.getHeight(null)==-1){}
			int width = image.getWidth(null);
			int height = image.getHeight(null);
			/*if(width==-1&&height==-1){
				width=DEFAULT_WIDTH;
				height=DEFAULT_HEIGHT;
				System.out.println("badsize picture : "+debugurl);
			}*/
			
			// This code ensures that all the pixels in the image are loaded
			image = new ImageIcon(image).getImage();
	
			// Determine if the image has transparent pixels; for this method's
			// implementation, see e661 Determining If an Image Has Transparent Pixels
			boolean hasAlpha = hasAlpha(image);
	
			//boolean hasAlpha = false;
	
			// Create a buffered image with a format that's compatible with the screen
			BufferedImage bimage = null;
			GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
			try {
				// Determine the type of transparency of the new buffered image
				int transparency = Transparency.OPAQUE;
				if (hasAlpha) {
					transparency = Transparency.BITMASK;
				}
	
				// Create the buffered image
				GraphicsDevice gs = ge.getDefaultScreenDevice();
				GraphicsConfiguration gc = gs.getDefaultConfiguration();
				try{
					bimage = gc.createCompatibleImage(
							width, height, transparency);
				}catch(IllegalArgumentException e){
					System.out.println(debugurl);
				}
			} catch (HeadlessException e) {
				// The system does not have a screen
			}
	
			if (bimage == null) {
				// Create a buffered image using the default color model
				int type = BufferedImage.TYPE_INT_RGB;
				if (hasAlpha) {
					type = BufferedImage.TYPE_INT_ARGB;
				}
				bimage = new BufferedImage(width, height, type);
			}
	
			// Copy image to buffered image
			Graphics g = bimage.createGraphics();
	
			// Paint the image onto the buffered image
			g.drawImage(image, 0, 0, null);
			g.dispose();
			return bimage;
		}else{
			return null;
		}
	}
//	This method returns true if the specified image has transparent pixels
	public static boolean hasAlpha(Image image) {
		// If buffered image, the color model is readily available
		if (image instanceof BufferedImage) {
			BufferedImage bimage = (BufferedImage)image;
			return bimage.getColorModel().hasAlpha();
		}

		// Use a pixel grabber to retrieve the image's color model;
		// grabbing a single pixel is usually sufficient
		PixelGrabber pg = new PixelGrabber(image, 0, 0, 1, 1, false);
		try {
			pg.grabPixels();
		} catch (InterruptedException e) {
		}

		// Get the image's color model
		ColorModel cm = pg.getColorModel();
		if(cm!=null){
			return cm.hasAlpha();
		}else{
			return false;
		}
	}
	public static void main(String[] args){
//		Write_Image("http://static.sanook.com/www/thumbs/100x60/10521.thumb.jpg"
//				,"D:\\tempppp\\temp.jpg");
		Write_Image("http://www.tarad.com/cartoon-v2d/img-lib/spd_20071209164633.jpg","D:\\tempppp\\temp.jpg");
	}
}
